/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wads.drawing.node;

import wads.app.*;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Rectangle2D;
import javax.swing.ImageIcon;

/**
 *
 * @author Nattasan W.
 */
public class GateNode extends CaseNode {
  
  private ImageIcon icon = new ImageIcon("images/notdefine32.png");
//  private Properties gateProp = new Properties();
  /* gateProp element name list
   * GateNode Type >> AND, OR, XOR, XOREVENT, COMPLEX
   * Object ID >> NodeID
   */
  private transient BaseWADDialog wGateDialog;

  public static final int DEFAULT = 0, AND = 1, OR = 2, XOR = 3, XOREVENT = 4, COMPLEX = 5;
  
//  public static final int DEFAULT_GATE_ICON_WIDTH = 30;
//  public static final int DEFAULT_GATE_ICON_HEIGHT = 30;
  //private static final int GAP = 10;

  private int gateType;
  
  public GateNode(Point anchor) {
    super(anchor, "Gate");
    
    id = ++idCount;
    
    //super.initContent();
    this.setWidth(DEFAULT_NODE_ICON_WIDTH);
    this.setHeight(DEFAULT_NODE_ICON_HEIGHT);
  }

  public GateNode(Point anchor, int type) {
    super(anchor, "Gate");

    id = ++idCount;

    gateType = type;
    setGateIconImage(gateType);

    //super.initContent();
    this.setWidth(DEFAULT_NODE_ICON_WIDTH);
    this.setHeight(DEFAULT_NODE_ICON_HEIGHT);
  }

//  @Override
//  public CaseNodeDialog createDialog() {
//    return null;
//  }

  public void setGateIconImage(ImageIcon ii) {
    icon = ii;
    System.gc();
  }

  public void setGateIconImage(int gatetype) {
      switch(gatetype) {
          case 1: setGateIconImage(new ImageIcon("images/and32.png")); break;
          case 2: setGateIconImage(new ImageIcon("images/or32.png")); break;
          case 3: setGateIconImage(new ImageIcon("images/xor32.png")); break;
          default: setGateIconImage(new ImageIcon("images/notdefine32.png")); break;
      }
  }
  
  @Override
  public void draw(Graphics2D g2d) {
    //Init normal box dimension
    int normBoxWidth = CaseNode.DEFAULT_NODE_ICON_WIDTH;
    int normBoxHeight = CaseNode.DEFAULT_NODE_ICON_HEIGHT;
	
    //Init the big box dimension
    //int bigBoxWidth=0;
    //int bigBoxHeight=0;

    //Init small box height
    //int smallBoxHeight = 15;
    //int smallBoxWidth = 40;

    //Init big box min height
    //int minBigBoxHeight = 30;

    //Init ascender line
    //int ascLineStartX = this.x + 2*this.gap;
    //int ascLineStartY = getStartY() + smallBoxHeight + GAP;

    //Get the text bounds
    g2d.setColor(Color.black);
    //Get bounds of the text

    setNodeShapeBounds2D(getStartX(), getStartY(), DEFAULT_NODE_ICON_WIDTH, DEFAULT_NODE_ICON_HEIGHT);
    removeAllComponents();

    Rectangle2D box = new Rectangle2D.Float(getStartX(), getStartY(), normBoxWidth, normBoxHeight);
    append(box);
    g2d.drawImage(icon.getImage(), getStartX(), getStartY(), DEFAULT_NODE_ICON_WIDTH, DEFAULT_NODE_ICON_HEIGHT, icon.getImageObserver());

    super.draw(g2d);
  }// end of draw()
  
  @Override
  public void showDialog(CaseEditor pf) {
    wGateDialog = new GateNodeDialog(pf, this);
    
    if(wGateDialog != null && wGateDialog instanceof BaseWADDialog) {
//      wGateDialog.setProperties(getGateProp());
      wGateDialog.initDisplay();
      wGateDialog.setVisible(true);
    }
  }

    /**
     * @return the gateType
     */
    public int getGateType() {
        return gateType;
    }

    /**
     * @param gatetype the gateType to set
     */
    public void setGateType(int gateType) {
        this.gateType = gateType;
    }

    public String getGateTypeInString() {
        switch(gateType) {
            case 1: return "AND";
            case 2: return "OR";
            case 3: return "XOR";
            case 4: return "XOREVENT";
            case 5: return "COMPLEX";
            default: return "DEFAULT";
        }
    }

    public void setGateTypeWithString(String type) {
        if(type.equals("AND")) {
            gateType = AND;
        }
        else if(type.equals("OR")) {
            gateType = OR;
        }
        else if(type.equals("XOR")) {
            gateType = XOR;
        }
        else if(type.equals("XOREVENT")) {
            gateType = XOREVENT;
        }
        else if(type.equals("COMPLEX")) {
            gateType = COMPLEX;
        }
        else {
            gateType = DEFAULT;
        }

        setGateIconImage(gateType);
    }

//  public Properties getGateProp() {
//    return gateProp;
//  }
//
//  public void setGateProp(Properties gateProp) {
//    this.gateProp = gateProp;
//  }
}
